package com.xinqi.modules.sys.dict.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.dict.domain.DictStageGradeEntity;
import com.xinqi.modules.sys.dict.dto.req.DictStageGradeQueryDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictGradeResultDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictStageGradeResultDTO;
import com.xinqi.modules.sys.dict.manager.DictStageGradeManager;
import com.xinqi.modules.sys.dict.mapper.DictStageGradeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 字典-学段年级管理层实现
 *
 * @author: linkai
 * @date: 2023/05/03
 */
@Service
@RequiredArgsConstructor
public class DictStageGradeManagerImpl extends BaseServiceImpl<DictStageGradeMapper, DictStageGradeEntity> implements DictStageGradeManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<DictStageGradeResultDTO> findPage(Page<Object> page, DictStageGradeQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<DictStageGradeResultDTO> findList(DictStageGradeQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 字典-学段年级数据
     */
    @Override
    public DictStageGradeEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 字典-学段年级数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public List<DictGradeResultDTO> findGradeByStageId(Long stageId) {
        return this.baseMapper.findGradeByStageId(stageId);
    }

    @Override
    public List<DictGradeResultDTO> findGradeByVersionId(Long versionId) {
        return this.baseMapper.findGradeByVersionId(versionId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addGrade(Long stageId, List<Long> gradeIdList) {
        // 删除之前
        delGrade(stageId);
        // 再添加
        if (CollectionUtils.isEmpty(gradeIdList)) {
            return true;
        }
        List<DictStageGradeEntity> list = gradeIdList.stream().map(v -> {
            DictStageGradeEntity entity = new DictStageGradeEntity();
            entity.setStageId(stageId);
            entity.setGradeId(v);
            return entity;
        }).collect(Collectors.toList());
        return this.saveBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delGrade(Long stageId) {
        LambdaQueryWrapper<DictStageGradeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictStageGradeEntity::getStageId, stageId);
        return this.baseMapper.delete(wrapper) > 0;
    }

}
